var ec_left3 = echarts.init(document.getElementById("l3"), 'my');

var option_left3 = {
    tooltip: {
        trigger: "axis",
    },
    dataZoom: [{
        type: "slider",
        show: false,
        startValue: 0,
        endValue: 4,
        yAxisIndex: [0],
    }],
    color: ["#2f89cf"],
    grid: {
        left: "10%",
        top: "10px",
        right: "10%",
        bottom: "4%",
        containLabel: true
    },
    yAxis: {
        type: 'category',
        data: [],
        axisLabel: {
            color: "rgba(255,255,255,.6)",
            fontSize: "12"
        },
        axisLine: {
            show: false
        },
        axisTick: {
            show: false
        }
    },
    xAxis: {
        type: 'value',
        axisLabel: {
            color: "rgba(255,255,255,.6)",
            fontSize: "12"
        },
        axisLine: {
            lineStyle: {
                color: "rgba(255,255,255,.1)",
            }
        },
        splitLine: {
            lineStyle: {
                color: "rgba(255,255,255,.1)"
            }
        }
    },
    series: [{
        data: [],
        type: 'bar',
        label: {
            show: true
        },
        showBackground: true,
        backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
        }
    }]
};

function get_l3_data() {
    $.ajax({
        url: "/l3",
        success: function (data) {
            console.log(data);
            if (data.length == 0) { // 添加判断，如果数据为空，则重新开始轮播
                option_left3.dataZoom[0].startValue = 0;
                option_left3.dataZoom[0].endValue = 4;
            } else {
                option_left3.yAxis.data = data.industry;
                option_left3.series[0].data = data.avg_market_value;
                if (option_left3.dataZoom[0].endValue == data.length) {
                    option_left3.dataZoom[0].endValue = 4;
                    option_left3.dataZoom[0].startValue = 0;
                } else {
                    option_left3.dataZoom[0].endValue = option_left3.dataZoom[0].endValue + 1;
                    option_left3.dataZoom[0].startValue = option_left3.dataZoom[0].startValue + 1;
                }
            }
            ec_left3.setOption(option_left3);
        },
        error: function (xhr, type, errorThrown) { }
    });
}

get_l3_data();
setInterval(get_l3_data, 2000);

window.addEventListener("resize", function () {
    ec_left3.resize();
});

// 将设置 option 的代码放在 setInterval 前面，使其不会一开始就显示最后一条数据
ec_left3.setOption(option_left3);
